// opIM.cpp : main source file for opIM.exe
//

#include "stdafx.h"


CAppModule _Module;

xmpp_mgr	*_pxmpp_client;

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);
	_pxmpp_client = new xmpp_mgr();
	_pxmpp_client->create_main();
	_pxmpp_client->create_login();
	_pxmpp_client->show_login();
	
	int nRet = theLoop.Run();
	_Module.RemoveMessageLoop();
	
	if(_pxmpp_client->login_sucessful()==false)
	{
		_Module.AddMessageLoop(&theLoop);
		//_pxmpp_client->show_main(SW_HIDE);
		_pxmpp_client->close_main();
		nRet = theLoop.Run();
		_Module.RemoveMessageLoop();
	}
	else
	{
		_Module.AddMessageLoop(&theLoop);
		_pxmpp_client->show_main(nCmdShow);
		nRet = theLoop.Run();
		_Module.RemoveMessageLoop();
	}
	delete	_pxmpp_client;
	_pxmpp_client = NULL;
	return nRet;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to 
// make the EXE free threaded. This means that calls come in on a random RPC thread.
//	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	ATLASSERT(SUCCEEDED(hRes));

	// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
	::DefWindowProc(NULL, 0, 0, 0L);

	AtlInitCommonControls(ICC_BAR_CLASSES);	// add flags to support other controls
	HMODULE hInstRich = NULL;
	hInstRich = LoadLibrary(L"riched20.DLL");
	hRes = _Module.Init(NULL, hInstance);
	ATLASSERT(SUCCEEDED(hRes));
	ULONG_PTR		gdiplusToken;
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);



	int nRet = Run(lpstrCmdLine, nCmdShow);

	_Module.Term();
	FreeLibrary(hInstRich );
	::CoUninitialize();
	Gdiplus::GdiplusShutdown(gdiplusToken);



	return nRet;
}
